import cn.neu.aimp.iot.constant.Constant;
import cn.neu.aimp.iot.entity.Device;
import cn.neu.aimp.iot.entity.RealplayConfig;
import cn.neu.aimp.iot.module.LoginAndOutModule;
import cn.neu.aimp.iot.module.RealPlayModule;
import cn.neu.aimp.iot.storage.ImgStorage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) throws IOException, InterruptedException {
        Device decice = LoginAndOutModule.login(Constant.HAIKANG, "192.168.1.61", 8000, "admin", "abc123..", 1L);
        RealplayConfig realplayConfig = new RealplayConfig();
//        realplayConfig.setIsImgStream(1);
//        Object[] objects = RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
//        ImgThread imgThread = new ImgThread((ImgStorage) objects[0]);
//        imgThread.start();
//        Thread.sleep(20000);
//        realplayConfig = new RealplayConfig();
        realplayConfig.setIsAutoControl(1);
        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
//        Thread.sleep(10000);
//        realplayConfig = new RealplayConfig();
//        realplayConfig.setIsImgStream(0);
//        RealPlayModule.postTasks(decice.getDeviceId(), realplayConfig, null, null);
//        RealPlayModule.snapshot(decice.getDeviceId(), "E:\\test\\ABCD.jpg");
    }


    static class ImgThread extends Thread{
        ImgStorage imgStorages;
        public ImgThread(ImgStorage imgStorages){
            this.imgStorages = imgStorages;
        }
        @Override
        public void run() {
            File f;
            for(int i=0;;i++){
                if(i%1000==0){
                    f = new File("E:\\test\\"+i+"MyFile.png");
                    BufferedImage image = imgStorages.consumePendingImg();
                    try {
                        ImageIO.write(image, "PNG", f);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
